package com.qmx.launcher;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.qmx.launcher.activity.ContactGroupActivity;
import com.qmx.launcher.activity.SettingActivity;
import com.qmx.launcher.activity.TeachLiveRoomActivity;
import com.qmx.launcher.activity.base.BaseActivity;
import com.qmx.launcher.bean.AuthModel;
import com.qmx.launcher.bean.LoginModel;
import com.qmx.launcher.bean.LzyResponse;
import com.qmx.launcher.callback.JsonCallback;
import com.qmx.launcher.constant.AppConstant;
import com.qmx.launcher.constant.ComParamContact;
import com.qmx.launcher.token.TokenManager;
import com.qmx.launcher.utils.SocketUtil;

public class LoginActivity extends BaseActivity {
    private Gson mGson = new Gson();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //        setContentView(R.fragment_meeting.activity_login);
        // 读取配置文件
        loadConfigData();

        // 申请动态权限
        checkPermission();
    }


    @Override
    public void onResume() {
        super.onResume();
        //        TokenManager.getInstance().clearAuth();
        if (TokenManager.getInstance().isLogin()) {
            Log.e("LoginActivity", TokenManager.getInstance().getAuthModel().getToken());
            //            afterUserSelected("5D1821014CF5599A");
            SocketUtil.getInstance();
            afterUserSelected(sharedPreferences.getString(AppConstant.USER_ID, ""));
            //
            getHttpToken();
            startActivity(new Intent(this, MeetingMainActivity.class));
            finish();
        } else {
            doActivate();
        }
    }

    private void doActivate() {
        //        5D1821064CF4519A	m1:0c03f-0bdf4-08110-045b0
        //        5A17491636FC39CB	m1:013cc-04853-01bc1-04063
        //        6D1821064CF3518C	s1:0bf46-06021-0815f-04764
        //        6D1821064CF5517D	s1:06881-085e1-0b057-0470f
        OkGo.<String>post(AppConstant.Url + ComParamContact.Activate.PATH)
                .params(ComParamContact.Activate.SN, "5D1821064CF4519A")
                .params(ComParamContact.Activate.SECRET, "m1:0c03f-0bdf4-08110-045b0")
                .params(ComParamContact.Activate.CLIENT_VERSION, "20.10")
                .params(ComParamContact.Activate.OS_VERSION, "20.15")
                .execute(new JsonCallback<String>() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.e("onSuccess", response.body() + "");
                        LzyResponse lzyResponse = mGson.fromJson(response.body(), LzyResponse.class);
                        if (lzyResponse.code == 2) {
                            doLogin();
                            Log.e("lzyResponsecode", lzyResponse.code + "");
                        } else if (lzyResponse.code == 1) {
                            LoginModel loginModel = mGson.fromJson(lzyResponse.data.toString(), LoginModel.class);
                            AuthModel authModel = new AuthModel();
                            authModel.setToken(loginModel.getUser_token().getToken());
                            TokenManager.getInstance().setAuthModel(authModel);

                            editor.putString(AppConstant.USER_ID, String.valueOf(loginModel.getId()));
                            editor.putString(AppConstant.IDENTIFIER, loginModel.getIdentifier());
                            editor.putString(AppConstant.USER_SIG, loginModel.getUser_sig().getUsersig());
                            editor.putString(AppConstant.MOBILE, loginModel.getMobile());
                            editor.putInt(AppConstant.ROOM_ID, loginModel.getRoom().getId());
                            editor.putString(AppConstant.ROOM_NAME, loginModel.getRoom().getName());
                            editor.putString(AppConstant.AVATAR, loginModel.getAvatar());
                            editor.commit();

                            //视频登录
                            SocketUtil.getInstance();
                            afterUserSelected(sharedPreferences.getString(AppConstant.USER_ID, ""));

                            getHttpToken();
                            startActivity(new Intent(LoginActivity.this, MeetingMainActivity.class));
                            finish();
                        }
                        //                      showToast(TokenManager.getInstance().getAuthModel().getToken());
                        //                            handleResponse(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        //                        showToast("接口有问题");
                        Log.e("onError", response.toString());
                        //                            handleError(response);
                    }
                });
    }

    private void doLogin() {
        OkGo.<LzyResponse<LoginModel>>post(AppConstant.Url + ComParamContact.Login.PATH)
                .params(ComParamContact.Login.ACCOUNT, "5D1821064CF4519A")
                .params(ComParamContact.Login.PASSWORD, "0c03f-0bdf4-08110-045b0")
                .execute(new JsonCallback<LzyResponse<LoginModel>>() {
                    @Override
                    public void onSuccess(Response<LzyResponse<LoginModel>> response) {
                        AuthModel authModel = new AuthModel();
                        authModel.setToken(response.body().data.getUser_token().getToken());
                        TokenManager.getInstance().setAuthModel(authModel);
                        editor.putString(AppConstant.USER_ID, String.valueOf(response.body().data.getId()));
                        editor.putString(AppConstant.IDENTIFIER, response.body().data.getIdentifier());
                        editor.putString(AppConstant.USER_SIG, response.body().data.getUser_sig().getUsersig());
                        editor.putString(AppConstant.MOBILE, response.body().data.getMobile());
                        editor.putInt(AppConstant.ROOM_ID, response.body().data.getRoom().getId());
                        editor.putString(AppConstant.ROOM_NAME, response.body().data.getRoom().getName());
                        editor.putString(AppConstant.AVATAR, response.body().data.getAvatar());
                        editor.commit();
                        //                        showToast(TokenManager.getInstance().getAuthModel().getToken());
                        //                                                startActivity(new Intent(LoginActivity.this, TeachMainActivity.class));
                        //视频登录
                        SocketUtil.getInstance();
                        afterUserSelected(sharedPreferences.getString(AppConstant.USER_ID, ""));

                        getHttpToken();
                        startActivity(new Intent(LoginActivity.this, MeetingMainActivity.class));
                        finish();
                        //                            handleResponse(response);
                    }

                    @Override
                    public void onError(Response<LzyResponse<LoginModel>> response) {
                        //                        showToast("接口有问题");
                        Log.e("onError", response.body().toString());
                        //                            handleError(response);
                    }
                });

    }
}
